{include file='ebcms@common/header' /}
{php}
	$default_attr = \ebcms\Config::get('content.attr');
	$where = [];
	if(input('attr')){
		$where = [
			'attr.attr' => input('attr'),
		];
	}
	$data = \app\content\model\Attr::with('content') -> where($where) -> order('attr.sort desc,attr.content_id desc') -> select();
{/php}
<script>
	var M = {};
	$(function() {
		$('.ebcms-thumb').hover(function() {
			M.thumb = art.dialog({
				title:'图片预览',
				padding:0,
				lock: false,
				fixed: false,
				drag: true,
				left: $(this).offset().left+$(this).width(),
				top: $(this).offset().top + $(this).height(),
				content: '<img src="'+$(this).data('src')+'" alt="" style="max-width:300px;max-height:300px;" />',
			}).show();
		}, function() {
			M.thumb.hide();
		});
		M.setattr = function(attr,attrs){
			var newattrs = attrs.replace(/ /g,',');  
			if (attrs == '') {
				attrs = [];
			}else{
				attrs = newattrs.split(',');
			}
			var index = attrs.indexOf(attr);
			if (-1 == index) {
				attrs.push(attr);
			}else{
				attrs.splice(index,1);
			}
			return attrs.join(',');
		}
		$('.ebcms-attr').bind('click', function(event) {
			var span = $('span',this);
			var id = $(this).data('id');
			var attr = [];
			$.each(span, function(index, val) {
				attr.push($(this).text());
			});
			var str = '<p>设置属性：<p/>';
			str += '<p><input type="text" class="form-control" style="width:300px;" value="'+attr.join(',')+'" id="new_attr"><p/>';
			var default_attr = {$default_attr|json_encode};
			str += '<p>';
			$.each(default_attr, function(index, val) {
				str += '<button type="button" class="btn btn-sm btn-danger" onclick="$(\'#new_attr\').val(M.setattr(\''+val+'\',$(\'#new_attr\').val()));">'+val+'</button>&nbsp;';
			});
			str += '<p/>';
			EBCMS.MSG.confirm(str,function(){
				EBCMS.CORE.submit({
					url:'{:url('admin.content/attr')}',
					queryParams:{
						id:id,
						attrs:$('#new_attr').val()
					},
					success:function(res){
						if (res.code) {
							location  = location;
						}else{
							EBCMS.MSG.alert(res.msg);
						}
					}
				});
			});
		});
	});
</script>
<div class="container-fluid">
	<div class="row">
		<div class="col-md-12">
			<h3>
				<span>推荐属性管理</span>
			</h3>
			<p>
				{php}
					$attrs = \think\Db::name('content_attr') -> group('attr') -> column('attr');
				{/php}
				{foreach name='attrs' item='vo'}
					<a href="{:url('index',['attr'=>$vo])}" class="btn btn-sm {eq name='Request.param.attr' value='$vo'}btn-danger active{else/}btn-primary{/eq}">{$vo}</a>
				{/foreach}
			</p>
			{if condition="input('attr')"}
				<div class="table-responsive">
					<table class="table table-bordered table-hover table-ebcms">
						<tbody>
							<tr>
								<th style="width: 80px;">选择</th>
								<th style="width: 80px;">排序</th>
								<th>标题</th>
								<th>属性</th>
								<th style="width: 50px;">状态</th>
								<th>浏览量</th>
								<th>创建时间</th>
							</tr>
							{foreach name='data' item='vo'}
							{php}
								$content = $vo['content'];
							{/php}
							<tr>
								<td>
									<label>
										<input type="checkbox" name="id" class="ebcms-id" value="{$vo.attr}_{$vo.content_id}">
										{$vo.content_id}
									</label>
								</td>
								<td>
									<input value="{$vo.sort}" class="form-control input-sm ebcms-change" url="{:url('resort')}" data-content_id="{$vo.content_id}" data-attr="{$vo.attr}">
								</td>
								<td>
									{neq name='content.thumb' value=''}
									<span class="ebcms-thumb glyphicon glyphicon-picture" style="color: red;cursor: pointer;" data-src="{$content.thumb|thumb}"></span>
									{/neq}
									<a href="{$content.url}" target="_blank">{$content.title}</a>
								</td>
								<td style="cursor: pointer;" class="ebcms-attr" data-id="{$content.id}">
									{foreach name='content.attr' item='v'}
									<span class="text-danger">{$v.attr}</span>
									{/foreach}
								</td>
								<td>
									{switch name='content.status'}
										{case value='99'}
											<span class="glyphicon glyphicon-question-sign text-warning" title="待审"></span>
										{/case}
										{case value='1'}
											<span class="glyphicon glyphicon-ok-sign text-primary" title="正常"></span>
										{/case}
										{default/}
											<span class="glyphicon glyphicon-ban-circle text-danger" title="隐藏"></span>
									{/switch}
								</td>
								<td>
									<span>{$content.click}</span>
								</td>
								<td>{$content.create_time|date='Y-m-d H:i:s',###}</td>
							</tr>
							{/foreach}
						</tbody>
						<tfoot>
							<tr>
								<td style="cursor: pointer;" onclick="$('input.ebcms-id').trigger('click');">
									<span class="glyphicon glyphicon-check"></span> 选择
								</td>
								<td colspan="7">
									<button onclick="$(this).data('attr',EBCMS.FN.getCheckedId('input.ebcms-id:checkbox:checked'));" type="button" class="btn btn-danger btn-sm ebcms-do" url="{:url('remove')}" confirm="移除并不会删除内容，移除吗？">移除</button>
									<span class="text-danger">建议定期将过久的内容从属性中移除出去以提升前台性能！</span>
								</td>
							</tr>
						</tfoot>
					</table>
				</div>
			{else/}
				<div class="alert alert-danger">
					<p>请选择属性以管理数据！</p>
					<p>建议定期将过久的内容从属性中移除出去以提升前台性能！</p>
				</div>
			{/if}
		</div>
	</div>
</div>
{include file='ebcms@common/footer' /}